//
//  PatternView.m
//  iOSStudy
//
//  Created by Evan Xie on 12/3/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import "PatternView.h"


#define kPatternCellWidth   16
#define kPatternCellHeight  18


@implementation PatternView


void ColorPatternDrawCallback(void *userInfo, CGContextRef myContext)
{
    CGFloat subunit = 5; // the pattern cell itself is 16 by 18
    
    CGRect  myRect1 = {{0,0}, {subunit, subunit}},
    myRect2 = {{subunit, subunit}, {subunit, subunit}},
    myRect3 = {{0,subunit}, {subunit, subunit}},
    myRect4 = {{subunit,0}, {subunit, subunit}};
    
    CGContextSetRGBFillColor (myContext, 0, 0, 1, 0.5);
    CGContextFillRect (myContext, myRect1);
    CGContextSetRGBFillColor (myContext, 1, 0, 0, 0.5);
    CGContextFillRect (myContext, myRect2);
    CGContextSetRGBFillColor (myContext, 0, 1, 0, 0.5);
    CGContextFillRect (myContext, myRect3);
    CGContextSetRGBFillColor (myContext, .5, 0, .5, 0.5);
    CGContextFillRect (myContext, myRect4);
}

void StencilPatternDrawCallback(void *userInfo, CGContextRef myContext)
{
    int k;
    double r, theta;
    
    r = 0.8 * kPatternCellWidth / 2;
    theta = 2 * M_PI * (2.0 / 5.0); // 144 degrees
    
    CGContextTranslateCTM (myContext, kPatternCellWidth/2, kPatternCellWidth/2);
    
    CGContextMoveToPoint(myContext, 0, r);
    for (k = 1; k < 5; k++) {
        CGContextAddLineToPoint (myContext,
                                 r * sin(k * theta),
                                 r * cos(k * theta));
    }
    CGContextClosePath(myContext);
    CGContextFillPath(myContext);
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    CGContextSaveGState(context);
    
    // Draw color pattern
    CGFloat alpha = 1.0;
    CGRect patternRect = CGRectMake(0, 0, rect.size.width, rect.size.height/2 - 10.0);
    CGPatternCallbacks callbacks = {0, ColorPatternDrawCallback, NULL};
    
    CGColorSpaceRef patternColorSpace = CGColorSpaceCreatePattern(NULL);
    CGContextSetFillColorSpace(context, patternColorSpace);
    CGColorSpaceRelease(patternColorSpace);
    
    CGPatternRef pattern = CGPatternCreate(NULL,
                                           CGRectMake(0, 0, kPatternCellWidth, kPatternCellHeight),
                                           CGAffineTransformMake(1, 0, 0, 1, 0, 0),
                                           kPatternCellWidth,
                                           kPatternCellHeight,
                                           kCGPatternTilingConstantSpacing,
                                           true,
                                           &callbacks);
    CGContextSetFillPattern(context, pattern, &alpha);
    CGPatternRelease(pattern);
    CGContextFillRect(context, patternRect);
    
    // Draw stencil pattern
    patternRect = CGRectMake(0, rect.size.height/2+10.0, rect.size.width, rect.size.height/2 - 10.0);
    CGColorSpaceRef baseColorSpace = CGColorSpaceCreateDeviceRGB();
    patternColorSpace = CGColorSpaceCreatePattern(baseColorSpace);
    CGContextSetFillColorSpace(context, patternColorSpace);
    CGColorSpaceRelease(baseColorSpace);
    CGColorSpaceRelease(patternColorSpace);
    
    callbacks = (CGPatternCallbacks){0, StencilPatternDrawCallback, NULL};
    static const CGFloat color[4] = { 0, 1, 0, 1 };
    
    pattern = CGPatternCreate(NULL,
                              CGRectMake(0, 0, kPatternCellWidth, kPatternCellWidth),
                              CGAffineTransformIdentity,
                              kPatternCellWidth,
                              kPatternCellWidth,
                              kCGPatternTilingConstantSpacingMinimalDistortion,
                              false,
                              &callbacks);
    CGContextSetFillPattern(context, pattern, color);
    CGContextFillRect(context, patternRect);
    CGPatternRelease(pattern);
    
    CGContextRestoreGState(context);
}
@end
